<template>
  <nav class="aui-navbar" :class="`aui-navbar--${$store.state.navbarLayoutType}`">
    <div class="aui-navbar__header">
      <h1 class="aui-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="aui-navbar__brand-lg" href="javascript:;">{{ $t('brand.lg') }}</a>
        <a class="aui-navbar__brand-mini" href="javascript:;">{{ $t('brand.mini') }}</a>
      </h1>
    </div>
    <div class="aui-navbar__body">
      <el-menu class="aui-navbar__menu mr-auto" mode="horizontal">
        <el-menu-item index="1" @click="$store.state.sidebarFold = !$store.state.sidebarFold">
          <svg class="icon-svg aui-navbar__icon-menu aui-navbar__icon-menu--switch" aria-hidden="true"><use xlink:href="#icon-outdent"></use></svg>
        </el-menu-item>
        <el-menu-item index="2" @click="refresh()">
          <svg class="icon-svg aui-navbar__icon-menu aui-navbar__icon-menu--refresh" aria-hidden="true"><use xlink:href="#icon-sync"></use></svg>
        </el-menu-item>
      </el-menu>
      <el-menu class="aui-navbar__menu" mode="horizontal">
        <el-menu-item index="4" @click="fullscreenHandle()">
          <svg class="icon-svg aui-navbar__icon-menu" aria-hidden="true"><use xlink:href="#icon-fullscreen"></use></svg>
        </el-menu-item>
        <el-menu-item index="6">
          <span class="el-dropdown-link" style="color: #333333">
              <div v-if="!isMeg" @click="messageTipHandle(true)">开启监听</div>
              <div v-if="isMeg">监听中
              <el-badge :value="msgLength" class="item" v-if="msgLength"></el-badge>
              </div>
            </span>
        </el-menu-item>
        <el-menu-item index="5" class="aui-navbar__avatar">
          <el-dropdown placement="bottom" :show-timeout="0">
            <span class="el-dropdown-link">
              <span>{{ $store.state.user.name }}</span>
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">{{ $t('updatePassword.title') }}</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">{{ $t('logout') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
      <audio :src="msgMp3" ref="audio" muted></audio>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePasswordVisible" ref="updatePassword"></update-password>
  </nav>
</template>
<script>
import screenfull from 'screenfull'
import UpdatePassword from './main-navbar-update-password'
import { clearLoginInfo } from '@/utils'
export default {
  inject: ['refresh'],
  data () {
    return {
      updatePasswordVisible: false,
      messageTip: false,
      isMeg: false,
      msgLength: 0,
      msgMp3:require('@/assets/msg.mp3'),
    }
  },
  components: {
    UpdatePassword
  },
  mounted () {
    let _this = this;
    setTimeout(function() {
      _this.messageTipHandle(true)
    }, 2000)
  },
  methods: {
    //消息提示
    messageTipHandle(is) {
      // console.log(is)
      let _this = this;
      this.isMeg = is
      if (!this.isMeg){
        return
      }
      // this.$notify.info({
      //   title: '订单',
      //   message: '您有新的订单消息',
      //   duration: 0,
      //   onClose: function() {
      //     console.log('已执行关闭按钮！')
      //   }
      // });
      // this.$refs['audio'].play()

      this.$http['get']('/api/massage/page', {params:{status:'未读',type:'',page: 1,limit: 10,}}).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (this.msgLength >= res.data.total){
          this.msgLength = res.data.total
          return
        }
        this.msgLength = res.data.total
        res.data.list.forEach((item, index) =>{
          _this.$notify.info({
            title: item.type,
            message: item.massage,
            duration: 0,
            onClose: function() {
              // console.log('已执行关闭按钮！')
              const item2 = {id: item.id, status: '已读'}
              _this.$http.put('/api/massage', item2).then(({data: res2}) => {
                if (res2.code !== 0) {
                  return this.$message.error(res2.msg)
                }
                // console.log(res2)
              }).catch(() => {
              })
            }
          });
          _this.$refs['audio'].play()
        })
      }).catch(() => {
      })
      setTimeout(function() {
        _this.messageTipHandle(_this.isMeg)
      }, 60000)
    },
    // 全屏
    fullscreenHandle () {
      if (!screenfull.enabled) {
        return this.$message({
          message: this.$t('fullscreen.prompt'),
          type: 'warning',
          duration: 500
        })
      }
      screenfull.toggle()
    },
    // 修改密码
    updatePasswordHandle () {
      this.updatePasswordVisible = true
      this.$nextTick(() => {
        this.$refs.updatePassword.init()
      })
    },
    // 退出
    logoutHandle () {
      this.$confirm(this.$t('prompt.info', { 'handle': this.$t('logout') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post('/logout').then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          clearLoginInfo()
          this.$router.push({ name: 'login' })
        }).catch(() => {})
      }).catch(() => {})
    }
  }
}
</script>
